<?php
/**
 * 好友管理
 * 
 * @author fanzhanao <fanzhanao@178.com>
 * @version $Id: data.php 12573 2011-05-12 21:21:46Z fanzhanao $
 */
class booklist_model_data extends cartoon_model_bmodel
{
	/**
	 * 最大好友数
	 *
	 * @var unknown_type
	 */
	private  $tb	= 'cartoon_booklist';
	
	private $tb_rel	=	'cartoon_booklist_rel';
	
	public function __construct()
	{
		parent::__construct();
	}
	
	/**
	 * 添加书单
	 * @param unknown_type $args
	 */
	public function add($args){
		if(!($args['title'] && $args['uid'])) return false;
		$args['flag'] = 1;
		$args['nums'] = 0;
		$args['add_time'] = CURRENT_TIME;
		//超过最大限度
		if($rs = $this->getCount($uid) && $rs > MAX_NUM) return ERR_TOO_MANY;
		if($this->chkTitle($args['uid'],$args['title'])) return ERR_EXITS;
		return $this->db->insert($this->tb,$args);
	}
	
	/**
	 * 修改书单名
	 * @param unknown_type $blid
	 * @param unknown_type $title
	 */
	public function edit($args){
//		return $this->db->modifyData($args,$this->tb,'id');
		return $this->db->update($this->tb,$args,array('id','title'));
	}
	
	/**
	 * 删除书单
	 * @param unknown_type $id
	 */
	public function delBookList($id,$uid){
		if(!$id) return false;
		$id = is_array($id)?implode(',',$id):$id;
		//删除书单
		if($this->db->query(" DELETE FROM $this->tb WHERE id IN ($id) AND uid = $uid" )){
			//更新书单中的漫画
			$this->db->query(" UPDATE $this->tb_rel SET blid=0 WHERE blid IN ($id) AND uid = $uid " );
			return true;
		}
	}
	
	/**
	 * 添加书单关系
	 * @param int $cid
	 * @param int $uid
	 * @param int $blid
	 */
	public function addRel($cid,$uid,$blid = 0){
		if(!($cid && $uid)) return Error::ERROR_WRONG_PARAMS ;
		if($this->chkExists($cid,$uid)) return Error::ERROR_RECORD_EXISTS ;
		$args = array('cid'=>$cid,'uid'=>$uid,'blid' => $blid,'dateline'=>CURRENT_TIME);
		if($this->db->insert($this->tb_rel,$args)){
			$this->db->query("UPDATE $this->tb SET nums = nums+1 WHERE id = $blid AND uid = $uid ");
			return true;
		}else return Error::ERROR_UNKNOWN ;
	}
	
	/**
	 * 删除关系
	 * @param int $cid
	 * @param int $blid
	 * @param int $uid
	 */
	public function removeRel($cid,$blid,$uid){
		if(!($cid && $blid )) return false;
		$cid = is_array($cid)?implode(',',$cid):$cid;
		$num	=	count(explode(',',$cid));
		//删除关系
		if($this->db->query("DELETE FROM $this->tb_rel WHERE cid IN( $cid ) AND blid = $blid AND uid = $uid ")){
			//跟新数量
			$sql	=	"UPDATE $this->tb SET nums = if( nums>$num,num-$num,0) WHERE id = $blid AND uid = $uid ";
			$this->db->query($sql);
			return true;
		}
	}
	
	/**
	 * 获取用户的当前已经有的书单
	 * @param unknown_type $uid
	 */
	public function getCount($uid){
		if(!$uid) return false;
		return $this->db->fetchField("SELECT COUNT(id) AS num FROM $this->tb WHERE uid = $uid",'num');
	}
	
	/**
	 * 检查书单是否已经存在
	 * @param unknown_type $uid
	 * @param unknown_type $title
	 */
	public function chkTitle($uid,$title){
		return $this->db->fetchField('SELECT id FROM '.$this->tb .' WHERE uid='.$uid.' AND tile=\''.$title.'\'','id')?true:false;
	}
	
	
	/**
	 * 获取书单信息
	 * @param unknown_type $id
	 */
	public function getRows($id){
		if(!$id) return false;
		$id = is_array($id)?implode(',',$id):$id;
		return $this->db->fetchRow("SELECT * FROM $this->tb WHERE id IN ($id)");
	}
	
	/**
	 * 检查是否已经加入书单了。
	 * @param unknown_type $cid
	 * @param unknown_type $uid
	 */
	public function chkExists($cid,$uid){
		if($cid && $uid){
			return $this->db->fetchField("SELECT id FROM $this->tb_rel WHERE cid = $cid AND uid = $uid ",'id');
		}
		return false;
	}
	
	/**
	 * 获取某人的书单列表
	 * @param unknown_type $uid
	 */
	public function getBookList($uid){
		if(!$uid) return false;
		$sql = "SELECT * FROM $this->tb WHERE uid =$uid ";
		$list = $this->db->fetchAll($sql);
		if(!empty($list)) return $list;
		else{/*
			if(!($list = $this->db->fetchAll($sql)))*/
			//初始化某人的书单列表
			$this->initBookList($uid);
			//重新获取书单
			return $this->getBookList($uid);
		}
//		return $list;
	}
	/**
	 * 初始化书单 ，如果用户开始没有书单的情况下，初始化默认书单
	 * @param unknown_type $uid
	 */
	public function initBookList($uid){
		if(!$uid) return false;
		$sql = "INSERT INTO $this->tb VALUES ";
		$value = $sep = '';
		foreach ($_SERVER['conf']['booklist'] as $list){
			$value .=$sep."(NULL,'$list',$uid,1,0,".CURRENT_TIME.")";
			$sep = ',';
		}
		$sql.=$value;
		return $this->db->query($sql);
	}
	
	/**
	 * 获取好友共享的
	 *
	 * @param unknown_type $friuids
	 * @return unknown
	 */
	public function getFriBlist($friuids){
		if(!$friuids) return false;
		$friuids = is_array($friuids)?implode(',',$friuids):$friuids;
		
		$sql = "SELECT * FROM $this->tb WHERE uid IN ($friuids) AND flag = 1 ORDER BY id DESC ";
	}
	
	/**
	 * 更改漫画所在的书单
	 * @param int $fromid
	 * @param int $toid
	 * @param int $uid
	 */
	public function changeBookListRel($cid,$fromid,$toid,$uid){
		if(!($cid && $fromid && $toid)) return false;
		$cid = is_array($cid)?implode(',',$cid):$cid;
		$cnt	=	count(explode(',',$cid));
		if($this->db->query("UPDATE $this->tb_rel SET blid = $toid WHERE cid IN( $cid ) AND blid = $fromid AND uid = $uid")){
			//更新原书单的数量
			$this->db->query("UPDATE $this->tb SET nums = if( nums>$cnt,nums-$cnt,0) WHERE id = $fromid AND uid = $uid ");
			//更新新书单的数量
			$this->db->query("UPDATE $this->tb SET nums = numS + $cnt WHERE id = $toid AND uid = $uid ");
			
			return true;
		}
		
		return false;
	}
	
	/**
	 * 获取书单的漫画列表
	 */
	public function getList($uid,$blid = null,$page = 1,$pageSize=16){
		$sql = "SELECT ct.id,ct.ct_title,ct.ct_cover,bl.blid FROM cartoon_cartoon ct,cartoon_booklist_rel bl
			WHERE ct.id = bl.cid AND bl.uid=$uid ";
		if($blid>0){
			$sql.=' AND bl.blid = '.$blid;
		}
		$sql.= ' ORDER BY bl.id DESC ';
		return $this->db->selectPage($sql,array(),$page,$pageSize);
	}
	
	/**
	 * 获取默认的书单的漫画数量
	 *
	 * @param unknown_type $uid
	 */
	public function getDefaultCount($uid){
		$sql = " SELECT COUNT(id) AS num FROM $this->tb_rel WHERE uid=$uid AND blid = 0";
		return $this->db->fetchField($sql,'num');
	}
	
}
